//
//  TLMyBonusTipsView.m
//  WXReader
//
//  Created by huaya on 2025/9/5.
//  Copyright © 2025 Andrew. All rights reserved.
//

#import "TLMyBonusTipsView.h"

@interface TLMyBonusTipsView ()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *centerHeightConst;
@property (weak, nonatomic) IBOutlet UIView *contentView;
@property (weak, nonatomic) IBOutlet UIButton *closeBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *contentCenterY;
@property (weak, nonatomic) IBOutlet UILabel *contentLab;

@end

@implementation TLMyBonusTipsView

+ (instancetype)createView {
    return [[[NSBundle mainBundle] loadNibNamed:@"TLMyBonusTipsView" owner:nil options:nil] lastObject];
}

- (void)awakeFromNib{
    [super awakeFromNib];
    self.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.6];
    self.contentCenterY.constant = kScreenHeight;
    self.contentView.layer.cornerRadius = 10;
    [self layoutIfNeeded];
}

//展示从底部向上弹出的UIView（包含遮罩）
- (void)showInView:(UIView *)view andContent:(NSString *)content
{
    if (!view)
    {
        return;
    }
    
    CGFloat requiredHeight = [self heightForHTMLText:content
                                           withWidth:(kScreenWidth-46*2)
                                            fontSize:13];
    
    // 设置富文本内容
    NSData *htmlData = [content dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                              NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)};
    
    NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:htmlData
                                                                           options:options
                                                                documentAttributes:nil
                                                                             error:nil];
    self.contentLab.attributedText = attributedString;
    
    self.centerHeightConst.constant = requiredHeight+40;
    
    [self layoutIfNeeded];
    
    
    [view addSubview:self];
    
    [UIView animateWithDuration:0.5 animations:^{
    
         self.alpha = 1.0;
        self.contentCenterY.constant = -45;
        [self layoutIfNeeded];
    } completion:nil];
}


//移除从上向底部弹下去的UIView（包含遮罩）
- (void)disMissView
{
//    [_contentView setFrame:CGRectMake(0, kDEVICEHEIGHT - 216, kDEVICEWIDTH, 216)];
    [UIView animateWithDuration:0.5f
                     animations:^{
        
        self.alpha = 0.0;
        self.contentCenterY.constant = kScreenHeight;
        [self layoutIfNeeded];
    }
                     completion:^(BOOL finished){
        
        [self removeFromSuperview];
        [_contentView removeFromSuperview];
        
    }];
 
}
- (IBAction)closeAction:(id)sender {
    [self disMissView];
    
}



// 计算显示 HTML 富文本的 UILabel 高度
- (CGFloat)heightForHTMLText:(NSString *)htmlText
                    withWidth:(CGFloat)width
                     fontSize:(CGFloat)fontSize {
    
    // 1. 将 HTML 字符串转换为 NSAttributedString
    NSAttributedString *attributedString;
    NSError *error = nil;
    
    NSData *htmlData = [htmlText dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *options = @{
        NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
        NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)
    };
    
    attributedString = [[NSAttributedString alloc] initWithData:htmlData
                                                       options:options
                                            documentAttributes:nil
                                                         error:&error];
    
    if (error) {
        NSLog(@"HTML parsing error: %@", error);
        return 0;
    }
    
    // 2. 创建 UILabel 并设置属性
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, CGFLOAT_MAX)];
    label.numberOfLines = 0;
    label.lineBreakMode = NSLineBreakByWordWrapping;
    label.attributedText = attributedString;
    
    // 3. 可选：设置字体大小（如果 HTML 中没有指定）
    if (fontSize > 0) {
        UIFont *font = [UIFont systemFontOfSize:fontSize];
        NSMutableAttributedString *mutableAttributedString = [attributedString mutableCopy];
        [mutableAttributedString addAttribute:NSFontAttributeName
                                        value:font
                                        range:NSMakeRange(0, mutableAttributedString.length)];
        label.attributedText = mutableAttributedString;
    }
    
    // 4. 计算所需尺寸
    CGSize size = [label sizeThatFits:CGSizeMake(width, CGFLOAT_MAX)];
    
    return ceil(size.height);
}


@end
